package com.ruoyi.vote.common.utils;

import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.ruoyi.common.config.XytpVoteConfig;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author 立勤
 * @date 2023/07/31 09:29:03
 */
public class CreatePayRequestUtil {

    private static final XytpVoteConfig xytpVoteConfig = SpringUtils.getBean(XytpVoteConfig.class);

    public static WxPayUnifiedOrderRequest createOrderRequest(HttpServletRequest request,String body, String notify, String orderNo, BigDecimal paymentPrice){
        WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest();
        wxPayUnifiedOrderRequest.setSignType(WxPayConstants.SignType.MD5);
        wxPayUnifiedOrderRequest.setBody(body);
        wxPayUnifiedOrderRequest.setTradeType(WxPayConstants.TradeType.JSAPI);
        wxPayUnifiedOrderRequest.setNotifyUrl(xytpVoteConfig.getMall().getNotifyHost()+notify);
        wxPayUnifiedOrderRequest.setOutTradeNo(orderNo);
        wxPayUnifiedOrderRequest.setTotalFee(BaseWxPayRequest.yuanToFen(String.valueOf(paymentPrice)));
        wxPayUnifiedOrderRequest.setSpbillCreateIp(ServletUtils.getClientIP(request));
        wxPayUnifiedOrderRequest.setOpenid(LoginHelper.getLoginUser().getOpenid());
        wxPayUnifiedOrderRequest.setTimeStart(DateUtils.dateTimeNow());     // 订单开始时间
        return wxPayUnifiedOrderRequest;
    }
}
